Classes and Methods עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון מבוסס על השקפים של אותו קורס שניתן בשנים הקודמות
2 Roadmap Lectures 4 and 5 present two sides of OOP: Lecture 4 discusses the static, compile time representation of object-oriented programs Lecture 5 discusses the dynamic, run time behavior Both are important, and both should be understood before we begin further investigation of object-oriented programming
3 Same Ideas, Different Terms All OOP languages have the following concepts, although the terms they use may differ: Classes, object type, factory object Instances, objects Message passing, method lookup, member function invocation, method binding Methods, member function, method function Inheritance, subclassing
Online Compiler Allows practicing new languages without the need to install an IDE. Everything you write is sent to the site. Don t write secret information. Don t use the site for solving assignments. Mark the private box. Otherwise, code you write is published on the site.
5 Objects as Examples of Abstract Data Types We noted that object-oriented programming, and objects in particular, can be viewed from many perspectives We described the many levels of abstraction from which one could examine a program In this chapter, we wish to view objects as examples of abstract data types
6 Encapsulation Programming that makes use of data abstractions is a methodological approach to problem solving where information is consciously hidden in a small parts of a program In particular, the programmer develops a series of abstract data types, each of which can be viewed as having two faces: push pop top const limit=300; var currenttop : 0.. limit; values : array [ 1.. limit ] of integer; The outside, or service view, describes what an object does The inside, or implementation view, describes how it does it
7 Encapsulation and Instantiation Classes provide a number of very important capabilities: Encapsulation The purposeful hiding of information, thereby reducing the amount of details that need to be communicated among programmers A Service View The ability to characterize an object by the service it provides, without knowing how it performs its task Instantiation The ability to create multiple instances of an abstraction
8 Instances and Instance Variables We have been using the term instance to mean a representative, or example, of a class We will accordingly use the term instance variable to mean an internal variable maintained by an instance Other terms we will occasionally use are: data field, or data members Each instance has its own collection of instance variables These values should not be changed directly by clients, but rather should be changed only by methods associated with the class Counter Instance Variable Counter value= value=
9 Behavior and State An object can also be viewed as a combination of behavior and state: Behavior: The actions that an instance can perform in response to a request Implemented by methods State: The data that an object must maintain in order to successfully complete its behavior Stored in instance variables (also known as data members, or data fields)
10 Class Definitions We will use as a running example the class definition for a playing card abstraction, and show how this appears in several languages Languages we will consider include Java, C++, C#, Delphi Pascal, Apple Pascal, Ruby, Python, Eiffel, Objective-C and Smalltalk
11 Programing Languages
12 C A general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie for use with the Unix operating system Although C was designed for implementing system software, it is also widely used for developing portable application software C is one of the most popular programming languages of all time and there are very few computer architectures for which a C compiler does not exist C has greatly influenced many other popular programming languages, most notably C++, which began as an extension to C
13 Class Definition C++ class PlayingCard { public: enum Suits {Spade, Diamond, Club, Heart}; Suits suit () { return suitvalue; } int rank () { return rankvalue; } private: Suits suitvalue; int rankvalue; }; public class PlayingCard { Java public enum Suit { Spade, Diamond, Club, Heart }; public int suit () { return suitvalue; }; public int rank () { return rankvalue; }; private int suitvalue; private int rankvalue;} enum Suits {Spade, Diamond, Club, Heart}; class PlayingCard { public Suits suit () { return suitvalue;} public int rank () { return rankvalue; } private Suits suitvalue; private int rankvalue; } C#
14 A Typical Example, Class Definition in C++ class PlayingCard { C++ public: enum Suits {Spade, Diamond, Club, Heart}; Suits suit () { return suitvalue; } int rank () { return rankvalue; } private: Suits suitvalue; int rankvalue; }; Note syntax for methods, instance variables, and visibility modifiers
15 Visibility Modifiers The terms public and private are used to differentiate the internal and external aspects of a class: Public features can be seen and manipulated by anybody They are the external (interface or service) view Private features can be manipulated only within a class They are the internal (implementation) view Typically methods are public and data fields are private, but either can be placed in either category
16 A C# Class Definition enum Suits {Spade, Diamond, Club, Heart}; C# class PlayingCard { public Suits suit () { return suitvalue; } public int rank () { return rankvalue; } } private Suits suitvalue; private int rankvalue; C# class definitions have minor differences: no semicolon at end enum cannot be nested inside class, and visibility modifiers are applied to methods and data fields individually
17 Java Class Definition public class PlayingCard { public enum Suit { Spade, Diamond, Club, Heart }; public int suit () { return suitvalue; }; public int rank () { return rankvalue; }; private int suitvalue; private int rankvalue;} Java also applies visibility modifiers to each item individually. The enum types are much more powerful than their counterparts in other languages: The enum declaration defines a class (called an enum type). The enum class body can include methods and other fields
18 Enumeration in Java: Example RED false Green true
19 Definition in Java 1.5- The int enum pattern Problems: Not typesafe - since a suit is just an int you can pass in any other int value where a suit is required, or add two suits together (which makes no sense) Printed values are uninformative - Because they are just ints, if you print one out all you get is a number, which tells you nothing about what it represents, or even what type it is. class PlayingCard { public static final int Spade = 1; public static final int Diamond = 2; public static final int Club = 3; public static final int Heart = 4; public int suit () { return suitvalue; } public int rank () { return rankvalue; } private int suitvalue; private int rankvalue; }
20 Static and Final Notice how symbolic constants are defined in Java: static means that all instance share the same value. One per class. Similar meaning in many languages final is Java specific, and means it will not be reassigned. (C++ has const keyword that is similar, although not exactly the same) public static final int Spade = 1; public static final int Diamond = 2; public static final int Club = 3; public static final int Heart = 4;
21 Pascal Pascal is an influential imperative and procedural programming language Designed in 1968/9 and published in 1970 by Niklaus Wirth As a small and efficient language intended to encourage good programming practices using structured programming and data structuring
22 Pascal Dialects We will consider two dialects of Pascal, both descended from the earlier language. Apple Object Pascal: Defined by Apple Computer, once widely used on the Macintosh, now much less commonly used Delphi Pascal: Defined by Borland on the PC, still fairly widely used on that platform. (Called Kylix on the Linux platform) Many similarities due to the common heritage, but some notable differences. We consider only the language aspects of Delphi, there are many other features related to its visual interface that we will not describe
23 Class Definition in Apple Object Pascal type Suits = (Heart, Club, Diamond, Spade); PlayingCard = object suit : Suits; rand : integer; end; No explicit visibility modifiers (We ll later see syntax for methods)
24 Delphi Pascal type Suits = (Heart, Club, Diamond, Spade); TPlayingCard = class (TObject) public constructor Create (r : integer; s : Suits); function suit : Suits; function rank : int; private suitvalue : Suits; rankvalue : integer; end; Slightly different syntax, must name parent class, has visibility modifiers and constructors (more on those later)
25 Smalltalk Smalltalk doesn't have a textual description for classes, but instead you define classes in a visual interface Revolutionary idea in 1980, but now Visual Basic and Delphi programmers are used to similar facilities
26 Methods In the next revision of our playing card abstraction we make the following changes: We add a method that will return the face color of the card, either red or black We add a data field to maintain whether the card is face up or face down, and methods both to test the state of this value and to flip the card
27 Methods Although syntax will differ depending upon language, all methods have the following: A name that will be matched to a message to determine when the method should be executed A signature, which is the combination of the name and argument types. Methods with the same name can be distinguished by different signatures A return type A body, which is the code that will be executed when the method is invoked in response to a message
28 An Example, from C# class PlayingCard { // constructor, initialize new playing card public PlayingCard (Suits is, int ir) { suit = is; rank = ir; faceup = true; } // operations on a playing card public boolean isfaceup () { return faceup; } public int rank () { return rankvalue; } public Suits suit () { return suitvalue; } public void setfaceup (boolean up) { faceup = up; } public void flip () { setfaceup(!faceup);} public Color color () { if ((suit() == Suits.Diamond) (suit() == Suits.Heart)) return Color.Red; return Color.Black; } } // private data values private Suits suitvalue; private int rankvalue; private boolean faceup;
29 Constructor class PlayingCard { // constructor, initialize new playing card public PlayingCard (Suits is, int ir) { suit = is; rank = ir; faceup = true; }... } A constructor is a method that is used to initialize a newly constructed object In C++, Java, C# and many other languages it has the same name as the class We will talk about constructors more in the next chapter
30 Accessor (or getter) Methods An accessor (or getter) is a method that simply returns an internal data value: class PlayingCard {... // operations on a playing card public int rank () { return rankvalue; } public Suits suit () { return suitvalue; }... private int rankvalue; }
31 Why Use an Accessor? There are many reasons why an accessor is preferable to providing direct access to a data field: You can make the data field read-only It provides better documentation that the data field is accessible It makes it easier to later change the access behavior (e.g., count number of accesses) Some conventions encourage the use of a name that begins with get, (as in getrank()), but this is not universally followed
32 Setters (or mutators) A setter (sometimes called a mutator method) is a method that is used to change the state of an object: class PlayingCard { } // operations on a playing card public void setfaceup (boolean up) { faceup = up; }... // private data values private boolean faceup; Mutators are less common than accessors, but reasons for using are similar
33 Order of Methods For the most part, languages don't care about the order that methods are declared. Here are some guidelines: List important topics first. Constructors are generally very important, list them first Put public features before private ones Break long lists into groups List items in alphabetical order to make it easier to search Remember that class definitions will often be read by people other than the original programmer Remember the reader, and make it easy for them
34 Constant Data Fields Some languages allow data fields to be declared as constant (const modifier in C++, final in Java, other languages have other conventions). Constant data fields can be declared as public, since they cannot be changed. class PlayingCard { // Java example... public static final int Spade = 1; public static final int Diamond = 2; public static final int Club = 3; public static final int Heart = 4; }
35 Separation of Definition and Implementation In some languages (such as C++ or Object Pascal) the definition of a method can be separated from its implementation. They may even be in different files: class PlayingCard { public:... Colors color () ;... }; PlayingCard::Colors PlayingCard::color ( ) { // return the face color of a playing card if ((suit == Diamond) (suit == Heart)) return Red; else return Black; } Notice need for fully-qualified names.
36 Considerations in Method Definitions In C++ you have a choice to define a method in the class interface, or separately in an implementation file. How do you decide? Readability Only put very small methods in the class definition, so that it is easier to read Semantics Methods defined in class interface may (at the discretion of the compiler) be expanded in-line Another reason for only defining very small methods this way.
37 Variations on Classes We will consider a few of the mostly languagespecific variations on the idea of a class: Methods without Implementations -- interfaces in Java Properties in Delphi and C# Nested classes in Java and C++ Class Data fields
38 Interfaces in Java An interface is like a class, but it provides no implementation. Later, another class can declare that it supports the interface, and it must then give an implementation. public interface Storing { void writeout (Stream s); void readfrom (Stream s); }; public class BitImage implements Storing { void writeout (Stream s) {... } void readfrom (Stream s) {... } }; We will have much more to say about interfaces later after we discuss inheritance
39 Properties A property is manipulated syntactically in the fashion of a data field, but operates internally like a method That is, a property can be read as an expression, or assigned to as a value: writeln ('rank is ', acard.rank); (* rank is property of card *) acard.rank= 5; (* changing the rank property *) However, in both cases the value assigned or set will be mediated by a function, rather than a simple data value Properties are a way to define getters and setters, but allow them to be used as if they were simple assignments and expressions
40 Properties in Delphi type TPlayingcard = class (TObject) public... property rank : Integer read rankvalue; property suit : Suits read suitvalue write suitvalue; end; private rankvalue : Integer; suitvalue : Suits; Here we have made rank read only, but allowed suit to be both read and written It is also possible to make a property write-only, although this is not very common
41 Properties in C# public class PlayingCard { public int rank { get { return rankvalue; } set { rankvalue = value; } }... private int rankvalue; } Omitting a set makes it read-only, omitting a get makes it write-only
42 Inner or Nested Classes Why Use Nested Classes? The nested class has a close conceptual relationship to its surrounding class It is a way of logically grouping classes that are only used in one place It increases encapsulation: It can lead to more readable and maintainable code
43 Inner or Nested Classes Some languages (C++ or Java) allow a class definition to be given inside another class definition Whether the inner class can access features of the outer class is different in different languages class LinkedList {... private class Link { // inner class public int value; public Link next; } }
44 Inner or Nested Classes Both Java and C++ allow the programmer to write one class definition inside of another Such a definition is termed an inner class in Java, and a nested class in C++ Despite the similar appearances, there is a major semantic difference between the two concepts An inner class in Java is linked to a specific instance of the surrounding class (the instance in which it was created), and is permitted access to data fields and methods in this object A nested class in C++ is simply a naming device, it restricts the visibility of features associated with the inner class, but otherwise the two are not related
45 Class Data Fields Idea is that all instances of a class can share a common data field Simple idea, but how to resolve the following paradox. All instances have the same behavior: Either they all initialize the common area, which seems bad, or Nobody initializes the common area, which is also bad Different languages use a variety of mechanisms to get around this
46 Class Data Fields Resolving this paradox requires moving outside of the simple class/method/instance paradigm Another mechanism, not the objects themselves, must take responsibility for the initialization of shared data If objects are automatically initialized to a special value (such as zero) by the memory manager, then every instance can test for this special value, and perform initialization if they are the first However, there are other (and better) techniques
47 Static modifier Class CountingClass { CountingClass () { count = count + 1; // increment count } private static int count; // shared by all } static { // static block count = 0; }
48 Static Modifiers In both C++ and Java shared data fields are created using the static modifier We have seen a use of this already in the creation of symbolic constants in Java In Java the initialization of a static data field is accomplished by a static block Which is executed when the class is loaded
49 Initialization of static data in C++ In C++ there are two different mechanisms: Data fields that are static (or const) and represented by primitive data types can be initialized in the class body, as we have seen already. Alternatively, a global initialization can be defined that is separate from the class: class CountingClass { public: CountingClass() { count++;... } private: static int count; }; // global initialization is separate from class int CountingClass::count = 0;
50 Chapter Summary In this chapter we have examined the static, or compile time features of classes: The syntax used for class definition The meaning of visibility modifiers (public and private) The syntax used for method definition Accessor or getter methods, and mutator or setter methods Variations on class themes Interfaces Properties Nested classes Class data fields